<template>
  <header
    ref="navbarHeader"
    class="fixed top-0 left-0 z-50 transition-all duration-100 overflow-hidden fixed-full-width"
    :class="{
      'py-4': !isScrolled,
    }"
    :style="{
      visibility: navbarVisible ? 'visible' : 'hidden',
    }"
    role="banner"
  >
    <nav ref="navbar" class="navbar bg-transparent transition-all duration-300" aria-label="主导航">
      <div
        class="container mx-auto px-4 sm:px-6 lg:px-8 flex items-center justify-between w-full h-12"
      >
        <!-- 左侧导航链接 -->
        <div
          ref="navLinks"
          class="nav-links hidden md:flex items-center space-x-6 lg:space-x-10 flex-shrink-0"
          role="navigation"
          aria-label="主菜单"
        >
          <router-link to="/gears" class="nav-link group">
            <span class="nav-link-text">GEARS</span>
            <div class="nav-link-underline"></div>
          </router-link>
          <router-link to="/journal" class="nav-link group">
            <span class="nav-link-text">JOURNAL</span>
            <div class="nav-link-underline"></div>
          </router-link>
          <router-link to="/support" class="nav-link group">
            <span class="nav-link-text">SUPPORT</span>
            <div class="nav-link-underline"></div>
          </router-link>
        </div>

        <!-- 中间Logo -->
        <div ref="navLogo" class="logo flex-shrink-0 center-with-scrollbar">
          <router-link to="/" class="logo-link group">
            <img src="/images/nav-logo.svg" alt="UNIGEAR" class="logo-img" />
            <div class="logo-underline"></div>
          </router-link>
        </div>

        <!-- 右侧图标 -->
        <div
          ref="navIcons"
          class="nav-icons flex items-center space-x-3 lg:space-x-6 flex-shrink-0"
          role="navigation"
          aria-label="用户操作"
        >
          <!-- 语言按钮 -->
          <button class="nav-icon-btn group" aria-label="语言选择">
            <img
              src="/images/nav-earth.svg"
              alt="语言"
              class="w-4 h-4 lg:w-5 lg:h-5 transition-transform duration-200 group-hover:scale-110"
              loading="lazy"
            />
          </button>
          <!-- 搜索按钮 -->
          <button class="nav-icon-btn group" aria-label="搜索">
            <svg
              class="w-4 h-4 lg:w-5 lg:h-5 transition-transform duration-200 group-hover:scale-110"
              fill="none"
              stroke="currentColor"
              viewBox="0 0 24 24"
            >
              <path
                stroke-linecap="round"
                stroke-linejoin="round"
                stroke-width="2"
                d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
              ></path>
            </svg>
          </button>

          <!-- 用户中心按钮 -->
          <button class="nav-icon-btn group" aria-label="用户中心">
            <svg
              class="w-4 h-4 lg:w-5 lg:h-5 transition-transform duration-200 group-hover:scale-110"
              fill="none"
              stroke="currentColor"
              viewBox="0 0 24 24"
            >
              <path
                stroke-linecap="round"
                stroke-linejoin="round"
                stroke-width="2"
                d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"
              ></path>
            </svg>
          </button>

          <!-- 购物车按钮 -->
          <button class="nav-icon-btn group relative" aria-label="购物车">
            <img
              src="/images/nav-baycar.svg"
              alt="购物车"
              class="w-4 h-4 lg:w-5 lg:h-5 transition-transform duration-200 group-hover:scale-110"
              loading="lazy"
            />
          </button>
        </div>

        <!-- 移动端菜单按钮 -->
        <button
          @click="toggleMobileMenu"
          class="md:hidden text-white focus:outline-none flex-shrink-0"
          aria-label="打开移动端菜单"
          aria-expanded="false"
          aria-controls="mobile-menu"
        >
          <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path
              stroke-linecap="round"
              stroke-linejoin="round"
              stroke-width="2"
              d="M4 6h16M4 12h16M4 18h16"
            ></path>
          </svg>
        </button>
      </div>
    </nav>
  </header>
</template>

<script setup>
import { ref, computed, onMounted, onBeforeUnmount } from 'vue'

// 响应式数据
const isMobileMenuOpen = ref(false)
const isScrolled = ref(false)
const windowWidth = ref(window.innerWidth)
const navbarVisible = ref(false)

// DOM引用
const navbarHeader = ref(null)
const navbar = ref(null)
const navLinks = ref(null)
const navLogo = ref(null)
const navIcons = ref(null)

// 计算属性
const isSmallScreen = computed(() => {
  return windowWidth.value < 768
})

// 导航栏准备函数
const prepareNavigation = () => {
  console.log('准备导航栏 - 设置为隐藏状态')
  navbarVisible.value = false
}

// 事件处理
const handlePrepareNavigation = () => {
  console.log('收到prepareNavigation事件')
  prepareNavigation()
}

const handleShowNavigation = () => {
  console.log('收到showNavigation事件')
  navbarVisible.value = true
  console.log('导航栏可见性设为true:', navbarVisible.value)
}

// 方法
const toggleMobileMenu = () => {
  isMobileMenuOpen.value = !isMobileMenuOpen.value
  document.body.style.overflow = isMobileMenuOpen.value ? 'hidden' : ''
}

const closeMobileMenu = () => {
  isMobileMenuOpen.value = false
  document.body.style.overflow = ''
}

const handleScroll = () => {
  const scrollY = window.scrollY
  isScrolled.value = scrollY > 50

  const header = navbarHeader.value
  if (header) {
    if (isScrolled.value) {
      header.classList.add('bg-black', 'bg-opacity-90', 'backdrop-blur-md', 'shadow-lg')
    } else {
      header.classList.remove('bg-black', 'bg-opacity-90', 'backdrop-blur-md', 'shadow-lg')
    }
  }
}

const handleResize = () => {
  windowWidth.value = window.innerWidth
  if (!isSmallScreen.value && isMobileMenuOpen.value) {
    closeMobileMenu()
  }
}

// 生命周期
onMounted(() => {
  console.log('NavBar组件挂载')

  // 立即准备导航栏
  prepareNavigation()

  window.addEventListener('prepareNavigation', handlePrepareNavigation)
  window.addEventListener('showNavigation', handleShowNavigation)
  window.addEventListener('scroll', handleScroll)
  window.addEventListener('resize', handleResize)

  handleScroll()
  handleResize()

  // 非首页立即显示导航栏
  if (window.location.pathname !== '/') {
    navbarVisible.value = true
  }
})

onBeforeUnmount(() => {
  window.removeEventListener('prepareNavigation', handlePrepareNavigation)
  window.removeEventListener('showNavigation', handleShowNavigation)
  window.removeEventListener('scroll', handleScroll)
  window.removeEventListener('resize', handleResize)
})
</script>

<style scoped>
/* 导航栏基础样式 - 防止横向滚动 */
.navbar {
  position: relative;
  z-index: 10;
  width: 100%;
  max-width: 100vw;
  overflow-x: hidden;
}

/* Header 统一高度和过渡效果 */
header {
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  box-sizing: border-box;
}

header.opacity-0 {
  pointer-events: none;
}

/* 容器样式优化 */
.container {
  width: 100%;
  max-width: var(--viewport-width);
  box-sizing: border-box;
  margin-left: auto;
  margin-right: auto;
}

/* Logo 绝对定位居中 */
.logo {
  z-index: 10;
  position: absolute;
  display: flex;
  align-items: center;
  height: 100%; /* 占据导航栏的全部高度 */
}

/* 导航链接样式 */
.nav-link {
  color: white;
  font-weight: 600;
  font-size: 0.875rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  position: relative;
  padding: 0.75rem 0;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  display: block;
  white-space: nowrap;
}

.nav-link-text {
  position: relative;
  z-index: 2;
  transition: color 0.3s ease;
}

.nav-link-underline {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 2px;
  background: linear-gradient(90deg, #ffffff 0%, rgba(255, 255, 255, 0.8) 100%);
  transition: width 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  border-radius: 1px;
}

.nav-link:hover .nav-link-underline {
  width: 100%;
}

.nav-link:hover .nav-link-text {
  color: rgba(255, 255, 255, 0.9);
  transform: translateY(-1px);
}

/* Logo 样式 - 确保Logo中心与左侧菜单中心对齐 */
.logo-link {
  display: flex;
  align-items: center;
  position: relative;
  padding: 0.75rem 0; /* 添加与nav-link相同的padding，确保下划线位置一致 */
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  outline: none;
}

.logo-img {
  height: 19px;
  width: auto;
  filter: brightness(0) invert(1);
  transition: all 0.3s ease;
  display: block;
  position: relative;
  z-index: 2;
}

/* Logo 下划线动画 - 从中间向两端展开 */
.logo-underline {
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 0;
  height: 2px;
  background: linear-gradient(90deg, #ffffff 0%, rgba(255, 255, 255, 0.8) 100%);
  transition: width 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  border-radius: 1px;
  transform: translateX(-50%); /* 从中心开始展开 */
}

/* Logo 悬停效果 - 与左侧菜单相同的动画风格 */
.logo-link:hover .logo-underline {
  width: 100%;
}

/* 移除Logo激活状态的下划线显示，只在悬停时显示 */

/* 图标按钮样式 */
.nav-icon-btn {
  color: white;
  padding: 0.5rem;
  border-radius: 0.5rem;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  position: relative;
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  flex-shrink: 0;
}

.nav-icon-btn:hover {
  background: rgba(255, 255, 255, 0.15);
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
  border-color: rgba(255, 255, 255, 0.2);
}

.nav-icon-btn:active {
  transform: translateY(0);
}

/* 防止内容溢出 */
* {
  box-sizing: border-box;
}
</style>
